﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text.RegularExpressions;
using web.Models;
using System.Text;

namespace web.Controllers
{
    public class BaseController : Controller
    {
        public userinfo loginuser = null;

        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);
            if (Session["user"] == null)
            {
                Response.Redirect("/Manager/Login");

            }
            else
            {
                loginuser = (userinfo)Session["user"];
            }

        }

        protected override JsonResult Json(object data, string contentType, Encoding contentEncoding)
        {
            return new CustomJsonResult { Data = data, ContentType = contentType, ContentEncoding = contentEncoding };
        }

        public new JsonResult Json(object data, JsonRequestBehavior jsonRequest)
        {
            return new CustomJsonResult { Data = data, JsonRequestBehavior = jsonRequest };
        }

        public new JsonResult Json(object data)
        {
            return new CustomJsonResult { Data = data, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        public siteinfo getsite()
        {

            if (HttpContext.Cache["site"] == null)
            {
                qydataEntities sce = new qydataEntities();
                HttpContext.Cache["site"] = sce.siteinfo.FirstOrDefault();
            }
            return (siteinfo)HttpContext.Cache["site"];
        }

        /// <summary>
        /// 请求form
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string RequestForm(string key)
        {
            if (Request.Form[key] == null)
            {
                return "";
            }
            else
            {
                return ReplaceBadChar(Request.Form[key].ToString());
            }
        }
        /// <summary>
        /// 请求url
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string RequestQ(string key)
        {
            if (Request.Params[key] == null)
            {
                return "";
            }
            else
            {
                return ReplaceBadChar(Request.Params[key].ToString());
            }
        }
        /// <summary>
        /// 过滤请求特殊字符
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public string ReplaceBadChar(string result)
        {
            return result.Replace("null", "").Replace("NaN", "");
        }
        public string JsonHelper(string json)
        {
            return Regex.Replace(json, @"\\/Date\((\d+)\)\\/", match =>
            {
                DateTime dt = new DateTime(1970, 1, 1);
                dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value));
                dt = dt.ToLocalTime();
                return dt.ToString("yyyy-MM-dd");
            });
        }
        public string JsonHelper(string json, string format)
        {
            return Regex.Replace(json, @"\\/Date\((\d+)\)\\/", match =>
            {
                DateTime dt = new DateTime(1970, 1, 1);
                dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value));
                dt = dt.ToLocalTime();
                return dt.ToString(format);
            });
        }

    }
}
